package com.lifeservice.console.service.impl;

import com.lifeservice.common.core.domain.entity.SysRole;
import com.lifeservice.common.core.domain.entity.SysUser;
import com.lifeservice.common.utils.*;
import com.lifeservice.console.domain.LifeOrder;
import com.lifeservice.console.domain.LifeSupplierInfo;
import com.lifeservice.console.mapper.LifeOrderMapper;
import com.lifeservice.console.service.ILifeOrderService;
import com.lifeservice.console.service.ILifeSupplierInfoService;
import com.lifeservice.system.service.ISysRoleService;
import com.lifeservice.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生活服务订单Service业务层处理
 *
 * @author lifeservice
 * @date 2023-02-25
 */
@Service
public class LifeOrderServiceImpl implements ILifeOrderService {
    @Autowired
    private LifeOrderMapper lifeOrderMapper;

    @Autowired
    private ILifeSupplierInfoService lifeSupplierInfoService;


    @Autowired
    private ISysUserService iSysUserService;


    /**
     * 查询生活服务订单
     *
     * @param id 生活服务订单主键
     * @return 生活服务订单
     */
    @Override
    public LifeOrder selectLifeOrderById(String id) {
        return lifeOrderMapper.selectLifeOrderById(id);
    }

    /**
     * 查询生活服务订单列表
     *
     * @param lifeOrder 生活服务订单
     * @return 生活服务订单
     */
    @Override
    public List<LifeOrder> selectLifeOrderList(LifeOrder lifeOrder,Boolean isExport) {

        String username = SecurityUtils.getLoginUser().getUsername();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        //根据用户id获取角色
        List<SysUser> sysUsers = iSysUserService.getUserByRoleName("供应商");
        List<LifeOrder> lifeOrders = new ArrayList<>();
        if (sysUsers.size() > 0) {
            if (sysUsers.stream().filter(s -> s.getUserId().equals(userId)).findAny().isPresent()) {
                //获取商户的id
                List<LifeSupplierInfo> lifeSupplierInfos = lifeSupplierInfoService.selectLifeSupplierInfoByName(username);
                if (CollectionUtils.isEmpty(lifeSupplierInfos)) {
                    return null;
                }
                List<String> supplierIds = new ArrayList<>();
                for (LifeSupplierInfo lifeSupplierInfo : lifeSupplierInfos) {
                    supplierIds.add(lifeSupplierInfo.getId());
                }
                if (!StringUtils.isEmpty(lifeOrder.getSupplierId()) && supplierIds.contains(lifeOrder.getSupplierId())) {
                    supplierIds.clear();
                    supplierIds.add(lifeOrder.getSupplierId());
                }
                lifeOrder.setSupplierIds(supplierIds);
                if(!isExport) {
                    PageUtils.startPage();
                }
                lifeOrders = lifeOrderMapper.selectLifeOrderListSupp(lifeOrder);
                lifeOrders.stream().forEach(item -> item.setIsSupplier(0));
                return lifeOrders;
            }

        }
        if(!isExport) {
            PageUtils.startPage();
        }
        lifeOrders = lifeOrderMapper.selectLifeOrderList(lifeOrder);
        lifeOrders.stream().forEach(item -> item.setIsSupplier(1));
        return lifeOrders;
    }

    /**
     * 新增生活服务订单
     *
     * @param lifeOrder 生活服务订单
     * @return 结果
     */
    @Override

    public int insertLifeOrder(LifeOrder lifeOrder) {
        lifeOrder.setId(SnowFlakeUtil.getInstance().nextId());
        lifeOrder.setCreateTime(DateUtils.getNowDate());
        return lifeOrderMapper.insertLifeOrder(lifeOrder);
    }

    /**
     * 修改生活服务订单
     *
     * @param lifeOrder 生活服务订单
     * @return 结果
     */
    @Override
    public int updateLifeOrder(LifeOrder lifeOrder) {
        lifeOrder.setUpdateTime(DateUtils.getNowDate());
        return lifeOrderMapper.updateLifeOrder(lifeOrder);
    }

    /**
     * 批量删除生活服务订单
     *
     * @param ids 需要删除的生活服务订单主键
     * @return 结果
     */
    @Override
    public int deleteLifeOrderByIds(String[] ids) {
        return lifeOrderMapper.deleteLifeOrderByIds(ids);
    }

    /**
     * 删除生活服务订单信息
     *
     * @param id 生活服务订单主键
     * @return 结果
     */
    @Override
    public int deleteLifeOrderById(String id) {
        return lifeOrderMapper.deleteLifeOrderById(id);
    }

    @Override
    public int declaration(LifeOrder lifeOrder) {
        LifeOrder lifeOrderKu = this.selectLifeOrderById(lifeOrder.getId());
        if (lifeOrderKu.getStatus().equals(lifeOrder.getStatus())) {
            return 1;
        }
        return lifeOrderMapper.declaration(lifeOrder);
    }
}
